package com.jingxi.rest.controller;

import com.jingxi.rest.JsonUtils;
import com.jingxi.rest.pojo.CatResult;
import com.jingxi.rest.service.ItemCatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJacksonValue;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class ItemCatController {
    @Autowired
    ItemCatService itemCatService;

    @RequestMapping(value = "/itemcat/list", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=utf-8")
    @ResponseBody
    public String getItemCatList(String callback) {
        CatResult catResult = itemCatService.getItemCatList();
        String json = JsonUtils.objectToJson(catResult);//JsonUtils是自定义工具类
        //拼装返回值
        String result = callback + "(" + json + ");";
        return result;
    }

//    @RequestMapping("/itemcat/list")
//    @ResponseBody
//    public Object getItemCatList(String callback) {
//        CatResult catResult = itemCatService.getItemCatList();
//        MappingJacksonValue mappingJacksonValue = new MappingJacksonValue(catResult);
//        mappingJacksonValue.setJsonpFunction(callback);
//        return mappingJacksonValue;
//    }
}
